﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BonsaiScrum.Bus;
using BonsaiScrum.Core.Query;
using BonsaiScrum.Mvc.Models;
using BonsaiScrum.Core.Command;
using BonsaiScrum.Core.Domain.Shared;
using BonsaiScrum.Core.Persistance;

namespace BonsaiScrum.Mvc.Controllers
{
    [Authorize]
    public class SprintController : Controller
    {
        //
        // GET: /Sprint/
        private ICommandBus _commandBus;
        private ISprintReportingRepository _sprintReportingRepository;
        private IProjectReportingRepository _projectReportingRepository;

        public SprintController() { }

        public SprintController(ICommandBus commandBus, ISprintReportingRepository sprintReportingRepository, IProjectReportingRepository projectReportingRepository)
        {
            _commandBus = commandBus;
            _sprintReportingRepository = sprintReportingRepository;
            _projectReportingRepository = projectReportingRepository;
        }

        public ActionResult Index(int projectId)
        {
            var proj = _projectReportingRepository.GetProjectWithId(projectId);
            if (proj != null)
            {
                var model = new SprintProj
                {
                    ProjectID = projectId,
                    Name = proj.Name,
                    Sprints = _projectReportingRepository.GetAllSprintsForProject(projectId)
                };
                return View(model);
            }
            else
                return RedirectToAction("Index", "Project");
            //var model = _sprintReportingRepository.GetAllSprintsInProject(projectId);
            //return View(model);
        }

        public ActionResult Create(int projectId)
        {
            var model = new CreateSprintModel
            {
                ProjectID = projectId
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateSprintModel model)
        {
            if (ModelState.IsValid)
            {
                var createSprintCommand = new CreateNewSprint(model.Start, model.End, model.Description, model.ProjectID, 1);
                _commandBus.Publish(createSprintCommand);

                return View("CreateSprintSuccess", model);
            }

            return View(model);
        }

        public ActionResult Update(int sprintId)
        {
            var model = _sprintReportingRepository.GetSprintWithId(sprintId);
            var tempmodel = new UpdateSprintModel
            {
                ProjectID = model.ProjectID,
                Description = model.Description,
                Start = model.StartDate,
                End = model.EndDate
            };

            return View(tempmodel);
        }

        [HttpPost]
        public ActionResult Update(UpdateSprintModel model)
        {
            if (ModelState.IsValid)
            {
                var updateSprintCommand = new UpdateSprint(model.Start, model.End, model.Description, model.ProjectID, 1);
                _commandBus.Publish(updateSprintCommand);

                return View("UpdateSprintSuccess", model);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Drop(int id)
        {
            var dropSprintCommand = new DropSprint(id);
            _commandBus.Publish(dropSprintCommand);

            return View("SprintHasBeenDropped");
        }
    }
}
